package com.shixi.shixitraining.shixitraining.ui.tabmine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.ResetPasswordBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

public class ChanagePasswordActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mBackImg;
    private EditText mOldPsw;
    private EditText mNewPsw;
    private EditText mComfirePsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {


        mBackImg = (ImageView) findViewById(R.id.iv_back);
        mOldPsw = (EditText) findViewById(R.id.edt_old_pw);
        mNewPsw = (EditText) findViewById(R.id.edt_new_pw);
        mComfirePsw = (EditText) findViewById(R.id.edt_comfire_pw);
        TextView mReset = (TextView) findViewById(R.id.reset_tv);
        mBackImg.setOnClickListener(this);
        mReset.setOnClickListener(this);


    }

    private void ResetPassword() {
        String oldPsw = mOldPsw.getText().toString();
        String newPsw = mNewPsw.getText().toString();
        String comfirePsw = mComfirePsw.getText().toString();
        if (TextUtils.isEmpty(oldPsw)){
           ToastUtil.showShortToast(ChanagePasswordActivity.this,"原密码不能为空");
            return;
        }else if (TextUtils.isEmpty(newPsw)){
            ToastUtil.showShortToast(ChanagePasswordActivity.this,"新密码不能为空");
            return;
        }else if (TextUtils.isEmpty(comfirePsw)){
            ToastUtil.showShortToast(ChanagePasswordActivity.this,"确认新密码不能为空");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("oldpsw", oldPsw);
        map.put("newPsw", newPsw);
        map.put("comfirePsw", comfirePsw);
        map.put("token", UserManager.getInstance().getToken());
        map.put("userId", UserManager.getInstance().getUserId());

        HttpUtil.getInstance().getApiInterface().reset(map).enqueue(new CallbackImple<ResetPasswordBean>() {
            @Override
            public void onError(Call<ResetPasswordBean> call, Throwable t) {
                ToastUtil.showShortToast(ChanagePasswordActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<ResetPasswordBean> call, ResetPasswordBean response) {
                if (response != null) {

                    if (response.getStatus()==200){
                        ToastUtil.showShortToast(ChanagePasswordActivity.this,"密码修改成功");
                        startActivity(new Intent(ChanagePasswordActivity.this,LoginActivity.class));
                    }else {
                     ToastUtil.showShortToast(ChanagePasswordActivity.this,response.getMsg());
                    }
                } else {
                    ToastUtil.showShortToast(ChanagePasswordActivity.this, "修改密码失败");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.reset_tv:
                if (!UserManager.getInstance().isUserLogged()){
                    ToastUtil.showShortToast(ChanagePasswordActivity.this,"您未登录");
                }else {
                    ResetPassword();
                }
                break;
        }
    }
}
